<template>
	<view class="container">
		<u-toast ref="uToast" />
		<u-navbar :is-back="true" title="编辑社团" back-icon-color='#333333' height="40" :border-bottom="false">
		</u-navbar>
		<u-mask :show="display">
			<view class="warp">
				<view class="rect" @tap.stop>
					<view class="tanchu">
						确认删除社团?
					</view>
					<view class="anniu">
						<view class="" @click="display=false">
							取消
						</view>
						<view class="" @click="deleteLeague">
							确定
						</view>
					</view>
				</view>
			</view>
		</u-mask>
		<u-select v-model="teacher" mode="mutil-column-auto" :list="teacher_list" @confirm="ensure"></u-select>
		<u-select v-model="show" mode="mutil-column-auto" :list="student_list" @confirm="commit"></u-select>
		<view class="list">
			<view class="description">
				<text>社团名称</text>
				<u-input v-model="form.name" style="margin-left:200rpx; width: 200rpx;" :type="type" placeholder="单行输入"
					:border="border" input-align="right" />
			</view>
			<view class="charge">
				<text>负责老师</text>
				<text>{{teacher_name}}</text>
				<text @click="teacher=true">选择老师</text>
			</view>
			<view class="proprieter">
				<text>社长</text>
				<text>(学生)</text>
				<text>{{master_student}}</text>
				<text @click="toggle(1)">选择学生</text>
			</view>
			<view class="participant">
				<text>社员</text>
				<text>{{club_person}}</text>
				<text @click="toggle(2)">选择学生</text>
			</view>
			<view class="abstract">
				<text>社团简介</text>
			</view>
			<view class=""
				style="width:94%; height:15rpx; margin-left:3%; background:#F2F2F2; border-top-left-radius:12rpx; border-top-right-radius:12rpx;">
			</view>
			<textarea value="" v-model="form.desc" placeholder="单行输入"
				style="width:94%; height:180rpx; margin-left:3%; background:#F2F2F2; text-indent:1em; border-bottom-left-radius:12rpx; border-bottom-right-radius:12rpx;" />
			<view class="request">
				<text>报考要求</text>
			</view>
			<view class=""
				style="width:94%; height:15rpx; margin-left:3%; background:#F2F2F2; border-top-left-radius:12rpx; border-top-right-radius:12rpx;">
			</view>
			<textarea value="" v-model="form.require" placeholder="单行输入"
				style="width:94%; height:180rpx; margin-left:3%; background:#F2F2F2; text-indent:1em; border-bottom-left-radius:12rpx; border-bottom-right-radius:12rpx;" />
			<view class="" style="width:100%; height:30rpx;">

			</view>
		</view>
		<view class="confirm">
			<view class="cancel" @click="display=true">
				{{cancel}}
			</view>
			<view class="verify" @click="leagueUpdate">
				{{verify}}
			</view>
		</view>
	</view>
</template>

<script>
	import request from '../../request.js'
	export default {
		data() {
			return {
				type: 'text',
				border: false,
				content: '',
				boolen: false,
				show: false,
				teacher: false,
				display: false,
				num: '',
				cancel: '删除',
				request_content: '',
				verify: '确认修改',
				input_content: '',
				teacher_name: '',
				master_student: '',
				club_person: '',
				form: {
					name: '',
					handle_teacher_user_id: '',
					handle_student_user_id: '',
					desc: '',
					require: '',
					student_user_ids: '',
					league_id: '',
				},
				student_list: [],
				teacher_list: [],
				club_list: [],
			}
		},
		onLoad(option) {
			this.gradeLists();
			this.groupLists();
			if (option) {
				this.form.league_id = option.id;
				this.teacher_name = option.teacher;
				this.master_student = option.header;
				this.club_person = option.student;
				this.form.name = option.club;
				this.form.desc = option.desc;
				this.form.require = option.require;
				this.form.handle_teacher_user_id = option.teacher_user_id;
				this.form.handle_student_user_id = option.student_user_id;
				console.log(option.clubId, option);
				this.form.handle_student_user_ids = option.clubId;
				this.club_person = option.clubName;

			}
		},
		methods: {
			ensure(e) {
				console.log(e);
				this.form.handle_teacher_user_id = e[1].value;
				this.teacher_name = e[1].label;
			},
			commit(e) {
				console.log(e);
				switch (this.num) {
					case 1:
						this.master_student = `${e[0].label}${e[1].label}${e[2].label}`;
						this.form.handle_student_user_id = e[2].value;
						break;
					case 2:
						this.club_person = `${e[0].label}${e[1].label}${e[2].label}`;
						this.club_list.push(e[2].value);
						this.form.student_user_ids = this.club_list.join(',');
						break;
				}
			},
			changeBoolen() {
				this.boolen = !this.boolen;
			},
			toggle(param) {
				this.show = true;
				switch (param) {
					case 1:
						this.num = 1;
						break;
					case 2:
						this.num = 2;
						break;
				}
			},
			leagueUpdate() {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'teacher/league/update',
					method: 'post',
					data: {
						...this.form,
					},
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data);
							_this.$toLink('../sign_club/sign_club', {}, 2);
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			deleteLeague() {
				this.display = false;
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'education/league/delete',
					data: {
						league_id: this.form.league_id,
					},
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data);
							_this.$toLink('../sign_club/sign_club', {}, 2);
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			gradeLists() {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'teacher/classes/gradeLists',
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data.data);
							res.data.data.forEach((item, index) => {
								_this.student_list.push({
									label: item.name,
									value: item.grade_id,
									children: [],
								})
								_this.classesLists(item.grade_id, index);

							})
							console.log(_this.student_list);
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			classesLists(param, param2) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let grade_id = param;
				let indexes = param2;
				let _this = this;
				request({
					url: base_url + 'teacher/classes/classesLists',
					method: 'post',
					data: {
						grade_id: grade_id
					},
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data.data);
							res.data.data.forEach((item, index) => {
								_this.student_list[param2].children.push({
									label: item.classes_name,
									value: item.classes_id,
									children: [],
								})
								_this.roomLists(grade_id, indexes, item.classes_id, index);
							})
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			roomLists(param1, param2, param3, param4) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'teacher/classes/classesStudent',
					method: 'post',
					data: {
						classes_id: param3
					},
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data.data);
							res.data.data.forEach(item => {
								if (_this.student_list[param2].children[param4]) {
									_this.student_list[param2].children[param4].children.push({
										label: item.full_name,
										value: item.user_id,
									})
								}
							})
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			groupLists() {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'office/group/lists',
					data: {
						school_district_id: uni.getStorageSync('school_district_id')
					},
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data.data.data);
							res.data.data.data.forEach((item, index) => {
								_this.teacher_list.push({
									label: item.name,
									value: item.subject_id,
									children: []
								})
								_this.userLists(item.subject_id, index);
							})

						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			userLists(param1, param2) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'office/group_user/lists',
					data: {
						subject_id: param1
					},
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data.data.data);
							res.data.data.data.forEach(item => {
								_this.teacher_list[param2].children.push({
									label: item.teacher_info ? item.teacher_info.full_name : '',
									value: item.teacher_user_id,
								})
							})

						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
		}

	}
</script>

<style lang="scss">
	.container {
		width: 100%;
		height: 100vh;
		background-color: #F2F2F2;

		.warp {
			display: flex;
			align-items: flex-start;
			justify-content: center;
			height: 100%;

			.rect {
				margin-top: 450rpx;
				border-radius: 10rpx;
				width: 480rpx;
				height: 250rpx;
				background-color: #fff;

				.tanchu {
					width: 100%;
					height: 150rpx;
					font-size: 33rpx;
					color: #3E3E3E;
					text-align: center;
					line-height: 150rpx;
				}

				.anniu {
					width: 100%;
					height: 100rpx;
					display: flex;
					justify-content: space-around;
					align-items: center;

					view {
						border-radius: 30rpx;
						width: 150rpx;
						height: 60rpx;
						text-align: center;
						line-height: 60rpx;
						font-size: 26rpx;

						&:nth-child(1) {
							color: #2C78FE;
							margin-left: 15rpx;
							border: 1px solid #2C78FE;
							background: #fff;

						}

						&:nth-child(2) {
							color: #fff;
							margin-right: 15rpx;
							background: linear-gradient(to right, #2C78FE, #32B0FF);
						}


					}
				}
			}
		}

		.list {
			background-color: #fff;
			margin-top: 20rpx;
			width: 710rpx;
			margin-left: 20rpx;
			border-radius: 12rpx;

			.description {
				width: 94%;
				margin-left: 3%;
				height: 90rpx;
				border-bottom: 1px solid #F2F2F2;
				display: flex;
				justify-content: flex-start;
				align-items: center;

				text {
					&:nth-child(1) {
						color: #666666;
						font-size: 26rpx;
					}
				}

			}

			.charge {
				width: 94%;
				margin-left: 3%;
				height: 90rpx;
				border-bottom: 1px solid #F2F2F2;
				display: flex;
				justify-content: flex-start;
				align-items: center;

				text {
					&:nth-child(1) {
						color: #666666;
						font-size: 26rpx;
						width: 150rpx;

					}

					&:nth-child(2) {
						color: #333333;
						font-size: 26rpx;
						width: 379rpx;
						text-align: right;

					}

					&:nth-child(3) {
						width: 107rpx;
						height: 40rpx;
						text-align: center;
						line-height: 40rpx;
						font-size: 20rpx;
						color: #333333;
						background-color: #EDF4FB;
						border: 1rpx solid #ccc;
						border-radius: 5rpx;
						margin-left: 20rpx;
					}
				}
			}

			.proprieter {
				width: 94%;
				margin-left: 3%;
				height: 90rpx;
				border-bottom: 1px solid #F2F2F2;
				display: flex;
				justify-content: flex-start;
				align-items: center;

				text {
					&:nth-child(1) {
						color: #666666;
						font-size: 26rpx;
						width: 80rpx;

					}

					&:nth-child(2) {
						color: #666666;
						font-size: 26rpx;
						width: 80rpx;
					}

					&:nth-child(3) {
						color: #333333;
						font-size: 26rpx;
						width: 369rpx;
						text-align: right;
					}

					&:nth-child(4) {
						width: 107rpx;
						height: 40rpx;
						text-align: center;
						line-height: 40rpx;
						font-size: 20rpx;
						color: #333333;
						background-color: #EDF4FB;
						border: 1rpx solid #ccc;
						border-radius: 5rpx;
						margin-left: 20rpx;
					}
				}
			}

			.participant {
				width: 94%;
				margin-left: 3%;
				height: 90rpx;
				border-bottom: 1px solid #F2F2F2;
				display: flex;
				justify-content: flex-start;
				align-items: center;

				text {
					&:nth-child(1) {
						color: #666666;
						font-size: 26rpx;

					}

					&:nth-child(2) {
						width: 477rpx;
						text-align: right;
						color: #333333;
						font-size: 26rpx;
					}

					&:nth-child(3) {
						width: 107rpx;
						height: 40rpx;
						text-align: center;
						line-height: 40rpx;
						font-size: 20rpx;
						color: #333333;
						background-color: #EDF4FB;
						border: 1rpx solid #ccc;
						border-radius: 5rpx;
						margin-left: 20rpx;
					}
				}
			}

			.abstract {
				width: 94%;
				margin-left: 3%;
				height: 85rpx;
				display: flex;
				justify-content: flex-start;

				text {
					&:nth-child(1) {
						color: #666666;
						font-size: 26rpx;
						margin-top: 30rpx;
					}
				}
			}

			.request {
				width: 94%;
				margin-left: 3%;
				height: 85rpx;
				display: flex;
				justify-content: flex-start;

				text {
					&:nth-child(1) {
						color: #666666;
						font-size: 26rpx;
						margin-top: 30rpx;
					}
				}
			}
		}

		.confirm {
			width: 100%;
			height: 250rpx;
			display: flex;
			justify-content: space-around;
			align-items: center;

			.verify {
				width: 200rpx;
				height: 60rpx;
				border-radius: 50rpx;
				text-align: center;
				line-height: 60rpx;
				background: linear-gradient(to right, #2C78FE, #32B0FF);
				color: #fff;
				font-size: 30rpx;
			}

			.cancel {
				width: 200rpx;
				height: 60rpx;
				border-radius: 50rpx;
				text-align: center;
				line-height: 60rpx;
				background: #fff;
				color: #666666;
				font-size: 30rpx;
			}
		}
	}
</style>
